{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "Introducing Cross-Validation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn import datasets\n",
    "\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "#load the classifying models\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.svm import SVC\n",
    "\n",
    "iris = datasets.load_iris()\n",
    "X = iris.data[:, :2]  #load the first two features of the iris data \n",
    "y = iris.target #load the target of the iris data\n",
    "\n",
    "#split the whole set one time\n",
    "#Note random state is 7 now\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=7)\n",
    "\n",
    "#split the training set into parts\n",
    "X_train_2, X_test_2, y_train_2, y_test_2 = train_test_split(X_train, y_train, test_size=0.25, random_state=7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,\n",
       "  decision_function_shape='ovr', degree=3, gamma='auto', kernel='linear',\n",
       "  max_iter=-1, probability=False, random_state=7, shrinking=True,\n",
       "  tol=0.001, verbose=False)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "svc_clf = SVC(kernel = 'linear',random_state = 7)\n",
    "svc_clf.fit(X_train_2, y_train_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "lr_clf = LogisticRegression(random_state = 7).fit(X_train_2, y_train_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy of SVC: 0.857142857143\n",
      "Accuracy of LR: 0.714285714286\n"
     ]
    }
   ],
   "source": [
    "svc_pred = svc_clf.predict(X_test_2)\n",
    "lr_pred = lr_clf.predict(X_test_2)\n",
    "\n",
    "print \"Accuracy of SVC:\",accuracy_score(y_test_2,svc_pred)\n",
    "print \"Accuracy of LR:\",accuracy_score(y_test_2,lr_pred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy of SVC on original Test Set:  0.684210526316\n"
     ]
    }
   ],
   "source": [
    "print \"Accuracy of SVC on original Test Set: \",accuracy_score(y_test, svc_clf.predict(X_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.82758621,  0.85714286,  0.92857143,  0.77777778])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.model_selection import cross_val_score\n",
    "svc_scores = cross_val_score(svc_clf, X_train, y_train, cv=4)\n",
    "svc_scores"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Average SVC scores:  0.847769567597\n",
      "Standard Deviation of SVC scores:  0.0545962864696\n"
     ]
    }
   ],
   "source": [
    "print \"Average SVC scores: \", svc_scores.mean()\n",
    "print \"Standard Deviation of SVC scores: \", svc_scores.std()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Average SVC scores:  0.748893906221\n",
      "Standard Deviation of SVC scores:  0.0485633168699\n"
     ]
    }
   ],
   "source": [
    "lr_scores = cross_val_score(lr_clf, X_train, y_train, cv=4)\n",
    "print \"Average SVC scores: \", lr_scores.mean()\n",
    "print \"Standard Deviation of SVC scores: \", lr_scores.std()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Average SVC scores:  0.827513227513\n",
      "Standard Deviation of SVC scores:  0.101857692405\n",
      "Score on Final Test Set: 0.815789473684\n"
     ]
    }
   ],
   "source": [
    "svc_scores = cross_val_score(svc_clf, X_train, y_train, cv=4)\n",
    "print \"Average SVC scores: \" , svc_scores.mean()\n",
    "print \"Standard Deviation of SVC scores: \", svc_scores.std()\n",
    "print \"Score on Final Test Set:\", accuracy_score(y_test, svc_clf.predict(X_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.model_selection import cross_val_score\n",
    "svc_scores = cross_val_score(svc_clf, X_train, y_train, cv = 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.model_selection import cross_val_score, StratifiedKFold\n",
    "skf = StratifiedKFold(n_splits = 4)\n",
    "svc_scores = cross_val_score(svc_clf, X_train, y_train, cv = skf)"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
